﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;


public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) //判断是否是回送，比如点击同一个页面的按钮
        {

            HttpCookie myCookie1 = Request.Cookies["username"];//获取Cookie信息
            if (myCookie1 != null)
            {
                //如果cookie存在，则将cookie信息即用户名显示出来
                string username = myCookie1.Value;
                TextBox1.Text = username;

            }
        }
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        string userid = TextBox1.Text;
        string pwd = TextBox2.Text;
        Session["id"] = userid;
        
        string conn = "Data Source= . ;Initial Catalog=XSXK; User ID=sa; pwd=123";
        SqlConnection connection = new SqlConnection(conn);
        try
        {
            connection.Open();
            SqlCommand comm = new SqlCommand();
            comm.Connection = connection;

            string str_sql = "select count(*) from [User]  where userid= '" + userid + "' and userpwd  = '" + pwd + "' ";
            comm.CommandText = str_sql;

            int count = Convert.ToInt32(comm.ExecuteScalar());
            if (count > 0)
            {//记录条数大于0，代表数据库有相应的用户名和密码存在
                Session["username"] = userid;//设置Session,用户实现登陆
                bool rem = CheckBox1.Checked;//判断有没有选中记住用户名
                HttpCookie SingleValueCookie = new HttpCookie("username");
                if (rem)
                {
                    //如果选中记住用户名，则将用户名记入Cookie，有效期设置为当前时间后1天
                    SingleValueCookie.Value = userid;
                    SingleValueCookie.Expires = System.DateTime.Now.AddDays(1);
                }
                else
                {
                    //如果未选中记住用户名，则将用户名Cookie有效期设置为当前时间1天之前
                    SingleValueCookie.Value = userid;
                    SingleValueCookie.Expires = System.DateTime.Now.AddDays(-1);
                }
                if (userid.Length > 5)
                {
                    Response.Cookies.Add(SingleValueCookie);//回送到客户端
                    Response.Redirect("Main_student.aspx", true);//跳转到登陆后主页面
                }
                else if(userid.Length == 4)
                {
                    Response.Cookies.Add(SingleValueCookie);//回送到客户端
                    Response.Redirect("Main_admin_JW.aspx", true);//跳转到登陆后主页面
                }
                else if (userid.Length == 5)
                {
                    Response.Cookies.Add(SingleValueCookie);//回送到客户端
                    Response.Redirect("Main_admin_XY.aspx", true);//跳转到登陆后主页面
                }
                else if (userid.Length == 2)
                {
                    Response.Cookies.Add(SingleValueCookie);
                    Response.Redirect("Main_teacher.aspx", true);//跳转到教师界面
                }
            }
            else
            {
                Response.Write("<h1>用户名或者密码输入错误</h1>");
            }


        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message);
        }

        finally
        {
            connection.Close();//关闭数据库连接
        }
    }


    
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {

    }


}